'use strict'
const path = require('path')
const AutoImport = require('unplugin-auto-import/webpack')
const Components = require('unplugin-vue-components/webpack')
const { ElementPlusResolver } = require('unplugin-vue-components/resolvers')
function resolve(dir) {
  return path.join(__dirname, dir)
}

const port = process.env.port || process.env.npm_config_port || 9526 // dev port

// const mock_server = require('./mock/mock-server')
module.exports = {
  publicPath: '/',
  outputDir: 'dist',
  assetsDir: 'static',
  //process.env.NODE_ENV === 'development', // eslint的报错是否显示在项目窗口
  lintOnSave: false,
  productionSourceMap: false,
  devServer: {
    port: port,
    open: false, // 是否打开浏览器
    overlay: {
      warnings: false, // eslint的warnings是否显示在项目窗口
      errors: false, // eslint的errors是否显示在项目窗口
    },
    // before: require('./mock/mock-server.js'),
    // proxy: 'http://103.90.155.19:8001',
    // proxy: 'http://103.90.155.19:33306', // 测试环境
    proxy: {
      '/dev': {
        target: 'http://103.90.155.19:33306',
        ws: true,
        changeOrigin: true,
        logLevel: 'debug',
        pathRewrite: {
          '/dev': '/',
        },
      },
      '/testing': {
        target: 'http://103.90.155.19:33308',
        ws: true,
        changeOrigin: true,
        logLevel: 'debug',
        pathRewrite: {
          '/testing': '/',
        },
      },
      '/guangzhoufuyou': {
        target: 'http://122.13.4.10:40004',
        ws: true,
        changeOrigin: true,
        logLevel: 'debug',
        pathRewrite: {
          '/guangzhoufuyou': '/',
        },
      },
      '/panyunancun': {
        target: 'http://14.215.43.6:20001',
        ws: true,
        changeOrigin: true,
        logLevel: 'debug',
        pathRewrite: {
          '/panyunancun': '/',
        },
      },
      '/zhongshanboai': {
        target: 'http://103.90.155.208:10014',
        ws: true,
        changeOrigin: true,
        logLevel: 'debug',
        pathRewrite: {
          '/zhongshanboai': '/',
        },
      },
      '/debugTesting': {
        target: 'http://103.90.155.19:33308',
        ws: true,
        changeOrigin: true,
        logLevel: 'debug',
        pathRewrite: {
          '/debugTesting': '/',
        },
      },
      '/hunan': {
        target: 'http://103.90.155.19:21193',
        ws: true,
        changeOrigin: true,
        logLevel: 'debug',
        pathRewrite: {
          '/hunan': '/',
        },
      },
      '/debugDongGuangRenMing': {
        target: 'http://103.90.155.208:20531',
        ws: true,
        changeOrigin: true,
        logLevel: 'debug',
        pathRewrite: {
          '/debugDongGuangRenMing': '/',
        },
      },
    },
  },
  configureWebpack: {
    name: process.env.VUE_APP_NAME || '全病程管理系统',
    resolve: {
      alias: {
        '@': resolve('src'),
        XMC: resolve('src/components/XMC'),
      },
    },
    module: {
      rules: [
        {
          test: /\.mjs$/,
          include: /node_modules/,
          type: 'javascript/auto',
        },
        // {
        //   test: /\.s[ac]ss$/i,
        //   use: [
        //     'style-loader',
        //     'css-loader',
        //     {
        //       loader: 'sass-loader',
        //       options: {
        //         additionalData: (content, loaderContext) => {
        //           // All scss files ending with imports.scss
        //           // will not re-import additionalData
        //           const { resourcePath } = loaderContext
        //           if (resourcePath.endsWith('target.scss')) return content
        //           // prepend import sentence
        //           // return `@import "@/target.scss";
        //           // ${content}`
        //           return `${content}`
        //         },
        //       },
        //     },
        //   ],
        // },
      ],
    },
    plugins: [
      // require('unplugin-auto-import/webpack')({
      //   resolvers: [ElementPlusResolver({ importStyle: false })],
      // }),
      // require('unplugin-vue-components/webpack')({
      //   resolvers: [ElementPlusResolver()],
      // }),
      // AutoImport({
      //   imports: ['vue', 'vue-router'],
      //   // 可以选择auto-import.d.ts生成的位置，使用ts建议设置为'src/auto-import.d.ts'
      //   // dts: 'src/auto-import.d.ts'
      // }),
      // require('unplugin-auto-import/webpack')({}),
    ],
  },
  chainWebpack(config) {
    // preload 预加载 提升首屏速度
    config.plugin('preload').tap(() => [
      {
        rel: 'preload',
        fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
        include: 'initial',
      },
    ])
    // 删掉预加载。当页面太多会导致太多无意义的需求，以后内网或者有提速需求可以考虑先开
    config.plugins.delete('prefetch')
    // svg-sprite-loader
    config.module.rule('svg').exclude.add(resolve('src/icons')).end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]',
      })
      .end()
    // 非开发环境用到的分包策略
    config.when(process.env.NODE_ENV !== 'development', (config) => {
      config
        .plugin('ScriptExtHtmlWebpackPlugin')
        .after('html')
        .use('script-ext-html-webpack-plugin', [
          {
            // `runtime` must same as runtimeChunk name. default is `runtime`
            inline: /runtime\..*\.js$/,
          },
        ])
        .end()
      config.optimization.splitChunks({
        chunks: 'all',
        cacheGroups: {
          libs: {
            name: 'chunk-libs',
            test: /[\\/]node_modules[\\/]/,
            priority: 10,
            // only package third parties that are initially dependent
            chunks: 'initial',
          },
          elementUI: {
            name: 'chunk-elementUI', // split elementUI into a single package
            /**
             * the weight needs to be larger than libs and app
             * or it will be packaged into libs or app
             */
            priority: 20,
            // in order to adapt to cnpm
            test: /[\\/]node_modules[\\/]_?element-ui(.*)/,
          },
          commons: {
            name: 'chunk-commons',
            test: resolve('src/components'), // can customize your rules
            minChunks: 3, //  minimum common number
            priority: 5,
            reuseExistingChunk: true,
          },
        },
      })
      // eslint-disable-next-line max-len
      // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
      config.optimization.runtimeChunk('single')
    })
  },
}
